package com.wangqi;

import org.apache.maven.plugin.logging.Log;
import org.codehaus.plexus.util.FileUtils;

import java.io.*;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;

public class JarProccessor {


    public void process(Map<String, ConfigFile> configMap, File jar, Log log) throws IOException {

        JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jar));
        File tmpJar = File.createTempFile(Long.toString(System.nanoTime()), ".jar");
        log.info("Tmp file: " + tmpJar.getAbsolutePath());
        JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(tmpJar));

        byte[] buffer = new byte[1024];
        log.info("----------------------");
        while (true) {
            JarEntry jarEntry = jarInputStream.getNextJarEntry();

            if (jarEntry == null) {
                break;
            }
            log.info(jarEntry.getName());
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            while (true) {
                int length = jarInputStream.read(buffer, 0, buffer.length);
                if (length <= 0) {
                    break;
                }
                byteArrayOutputStream.write(buffer, 0, length);
            }

            boolean filtered = false;
            ConfigFile cf = configMap.remove(jarEntry.getName());
            if (cf == null) {
                jarOutputStream.putNextEntry(jarEntry);
                byteArrayOutputStream.writeTo(jarOutputStream);
            } else {
                JarEntry filteredJarEntry = new JarEntry(jarEntry.getName());
                jarOutputStream.putNextEntry(filteredJarEntry);
                byte[] content = cf.getContent();
                if (content.length > 0) {
                    jarOutputStream.write(content, 0, content.length);
                }
                log.info(String.format("Replacing: %s!%s", jar.getName(), jarEntry.getName()));
            }

        }
        //The additional files.
        for (Map.Entry<String, ConfigFile> entry : configMap.entrySet()) {
            JarEntry filteredJarEntry = new JarEntry(entry.getKey());
            jarOutputStream.putNextEntry(filteredJarEntry);
            byte[] content = entry.getValue().getContent();
            if (content.length > 0) {
                jarOutputStream.write(content, 0, content.length);
            }

            log.info(String.format("addition: %s", entry.getKey()));
        }

        if (jarInputStream != null) {
            try {
                jarInputStream.close();
            } catch (Exception e) {
            }
        }
        if (jarOutputStream != null) {
            try {
                jarOutputStream.close();
            } catch (Exception e) {
            }
        }

        log.info("Replacing: " + jar.getAbsolutePath() + " with: " + tmpJar.getAbsolutePath());
        FileUtils.copyFile(tmpJar, jar);
        FileUtils.forceDeleteOnExit(tmpJar);
    }
}
